package fields;
import game.Player;

public abstract class Ownable extends Field {
	
	private int price;
	private boolean pawned;
	protected Player owner;
	
	public Ownable(){
		super(0,"");
		this.price = 0;
		pawned = false;
		owner = null;
	}
	
	public Ownable (int number, String name, int price)
	{
		super (number,name);
		this.price=price;
		pawned=false;
		owner=null;
	}
	
	public abstract int getRent();
	
	public void buy(Player buyer) {
		buyer.buyProperty(this, price);
		setOwner(buyer);
	}
	
	public int getPrice() {
		return price;
	}
	
	public boolean isOwnable()
	{
		if (owner==null) return true;
		return false;
	}
	
	public Player getOwner() {
		return owner;
	}
	
	public void setOwner(Player s)
	{
		owner=s;
	}
	
	public boolean isOwner(Player p)
	{
		if (owner!=null && getOwner().equals(p.getName())) return true;
		return false;
	}
	
	public String getOwnerName()
	{
		return owner.getName();
	}
	
	public void forSale()
	{
		owner=null;
	}
	
	public String fieldInfo(Player p) {
		if (pawned) return "This field is pawned!";
		if (isOwnable()) return this.getName()+" is ownable!";
		else if (owner.isInJail()) return "The owner is in jail. It is free to park.";
		else if (p.isOwner(this)) return "You are the owner of "+this.getName();
		else return this.getName()+" is owned by "+this.getOwnerName()+". Pay "+getRent();
	}
	
	public boolean isPawned() {
		return pawned;
	}
	
	public void pawn() {
		if (!isOwnable()) {
			owner.deposit(price/2);
			pawned=true;
		}
	}
	
	public void setPawned() {
		if (!isOwnable()) pawned=true;
	}
	
	public void unpawn() {
		if (!isOwnable() && pawned) {
			owner.withdraw((int)((price/2)*1.1));
			pawned=false;
		}
	}
}
